package com.oig.sys.auth.dao;

import com.oig.sys.auth.entity.TbRoleSystem;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
@author wuxl
*/
public interface ITbRoleSystemDao extends JpaRepository<TbRoleSystem, Integer> {

	TbRoleSystem getByRoleIdAndSysId(Integer roleId, Integer sysId);

	@Query("select rs.id from TbRoleSystem rs where rs.roleId=:roleId")
	List<Integer> findAllIdByRoleId(@Param("roleId") Integer roleId);

	@Query("select rs.id from TbRoleSystem rs where rs.sysId=:sysId")
	List<Integer> findAllIdBySystemId(@Param("sysId") Integer sysId);

	@Query("select rs.id from TbRoleSystem rs where rs.roleId=:roleId and rs.id not in(:idList)")
	List<Integer> findAllDelId(@Param("roleId") Integer roleId, @Param("idList") List<Integer> idList);

	@Modifying
	@Query("delete from  TbRoleSystem rs where rs.id in(:idList)")
	void deleteByIdList(@Param("idList") List<Integer> idList);

	@Query("select rs.sysId from TbRoleSystem rs where rs.roleId=:roleId")
	List<Integer> findSystemIdByRole(@Param("roleId") Integer roleId);

	@Query("select rs.sysId from TbRoleSystem rs inner join TbSystem s on s.id=rs.sysId" +
			" where rs.roleId in(:roleIdList) and s.status=:status")
	List<Integer> findSystemIdByRole(@Param("roleIdList") List<Integer> roleIdList, @Param("status") String status);

}
